package com.sx.micowell.module.trainplan

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.app.TimePickerDialog
import android.text.TextUtils
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.mvvm.base.data.DataBindingConfig
import com.example.mvvm.until.LogUtils
import com.example.mvvm.until.ToastUtils
import com.gyf.immersionbar.ImmersionBar
import com.miloyu.modelnet.request.AddTrainPlanDTO
import com.miloyu.modelnet.request.TrainPlanInfo
import com.miloyu.modelnet.response.DictType
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.ActSelectTrainTypeBinding
import com.sx.micowell.framework.app.IntentKey
import com.sx.micowell.framework.app.MyActivity
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.model.TrainPlanModel
/**
 * @Author: YY
 * @Time: 2021/8/24
 * @Describe 选择训练类型
 */
@Route(path = RoutePath.SELECT_TRAIN_TYPE)
class SelectTrainTypeActivity : MyActivity<ActSelectTrainTypeBinding>() {

    @Autowired(name = IntentKey.DATA)
    @JvmField
    var startTime : String? = null

    @Autowired(name = IntentKey.ID)
    @JvmField
    var endTime : String? = null

    lateinit var viewModel: TrainPlanModel

    private var trainTypeListAdapter : TrainTypeListAdapter? = null

    override fun initViewModel() {
        viewModel = getActivityScopeViewModel(TrainPlanModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.act_select_train_type, BR.viewModel, viewModel)
    }

    override fun initView() {
        ImmersionBar.with(this).statusBarDarkFont(true)
            .fitsSystemWindows(true).init()
    }

    @SuppressLint("SetTextI18n")
    override fun initData() {
        // 训练时间
        binding.tvContent.text = "$startTime - $endTime"
        // 获取训练类型
        viewModel.queryTrainType()

        // 点击保存
        binding.btnSave.setOnClickListener {
            val mutableList = mutableListOf<TrainPlanInfo>()
            var weekStr: String?=""
            trainTypeListAdapter?.data?.forEach {
                if (it.isSelect) {
                    it.weeks.forEach { data->
                        if (data.isSelect) {
                            weekStr = "${data.dictValue},$weekStr"
                        }
                    }
                    if (TextUtils.isEmpty(it.trainTime)) {
                        ToastUtils.showShort("请先设置运动时长")
                        return@setOnClickListener
                    } else if (TextUtils.isEmpty(weekStr)) {
                        ToastUtils.showShort("请选择星期")
                        return@setOnClickListener
                    }
                    val trainPlanDTO = TrainPlanInfo(it.trainTime, it.dictValue, weekStr?.trim())
                    mutableList.add(trainPlanDTO)
                    weekStr = ""
                }
            }
            val dto = AddTrainPlanDTO(startTime, endTime, mutableList)
            viewModel.addTrainPlan(dto)
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        setTrainTypeList()
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun setTrainTypeList() {
        //取消recyclerview单独的滑动效果
        binding.recyclerType.isNestedScrollingEnabled = false
        //设置布局管理器
        binding.recyclerType.layoutManager = LinearLayoutManager(context)

        viewModel.trainTypeEvent.observe(this, {

            trainTypeListAdapter = TrainTypeListAdapter()
            trainTypeListAdapter?.data = it
            binding.recyclerType.adapter = trainTypeListAdapter

            trainTypeListAdapter?.addChildClickViewIds(R.id.imageView1, R.id.textView1)
            trainTypeListAdapter?.setOnItemChildClickListener { adapter, view, position ->
                val dictType: DictType = it[position]
                if (view.id == R.id.imageView1) {
                    dictType.isSelect = !dictType.isSelect
                    adapter.notifyItemChanged(position)
                } else {
                    TimePickerDialog(this, AlertDialog.THEME_HOLO_LIGHT, { view, hourOfDay, minute ->
                            if ((hourOfDay * 60) + minute == 0) {
                                ToastUtils.showShort("运动时长须大于1分钟")
                                return@TimePickerDialog
                            }
                            dictType.trainTime = ((hourOfDay * 60) + minute).toString()
                            LogUtils.d("onTimeSet: ${dictType.trainTime}")
                            adapter.notifyItemChanged(position)
                        }, 0,0,true).show()
                }

            }
        })
    }
}